﻿package cn.valorin.command;

import static cn.valorin.configuration.languagefile.MessageSender.sm;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cn.valorin.Main;
import cn.valorin.command.sub.MainHelp;
import cn.valorin.command.way.AdminCommands;
import cn.valorin.command.way.InServerCommands;

public class CommandsExecutor implements CommandExecutor {
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) 
    {
      Player p = null;
      if (sender instanceof Player)
      { p = (Player)sender; }
      if (args.length == 0)
      {
    	return MainHelp.onCommand(sender, command, label, args);
      } else {
    	SubCommand subCommand = Main.getInstance().getCommandsHandler().getSubCommand(args[0]);
        if (subCommand == null) 
        {
          sm("&c不存在此则子指令，请检查输入",p);
          return true;
        }
        if (subCommand instanceof AdminCommands)
        {
          if (!sender.hasPermission("duanwu.admin"))
          {
        	sm("&c无权限！",p);
        	return true;
          }
          return subCommand.onCommand(sender, command, label, args);
        } else if (subCommand instanceof InServerCommands) {
          if (!(sender instanceof Player))
          {
            sm("&c这条指令只能由服务器内的玩家执行！后台无法使用！",p);
            return true;
          }
          return subCommand.onCommand(sender, command, label, args);
        } else {
          return subCommand.onCommand(sender, command, label, args);
        }
      }
    }
}
